/************************************************************************
Purpose: Plot Histograms of Permit Consumption (i.e. Emissions).
*************************************************************************/

set more off
clear all
pause on

local figure "`1'"

use "$TRADING_DATA_CLEAN/panel_plant-period.dta", clear

if "`figure'" == "F3" {
	* Use unadjusted emissions (see footnote 22 in the main text)
	merge 1:1 gpcb_id period_num using "$TRADING_DATA_CLEAN/panel_plant-period_unadjusted_emissions.dta", ///
	assert(master match) nogen
	
	* Confirm 37 plants need emissions unadjusted
	qui: count if !missing(emission_val_noadjust_prorated)
	assert `r(N)' == 37
	
	replace emission_val_prorated = emission_val_noadjust_prorated if !missing(emission_val_noadjust_prorated)
	drop emission_val_noadjust_prorated
}

** Normalized emissions
gen emission_per_alloc = emission_val_prorated / permit_alloc_prorated * 100
gen emission_per_holding = emission_val_prorated / permit_hold_prorated * 100


********************************************************************************
*** Emissions / Allocation 
********************************************************************************
if "`figure'" == "3" {

	forval i=1/10 {
		
		preserve
		
		keep if period_num == `i'
		
		** Drop if emission is negative
		keep if emission_val_prorated >= 0
		
		** Truncate at 500%
		keep if emission_per_alloc < 500
		
		hist emission_per_alloc, ///
			width(25) start(0)  xscale(range(0 500)) ///
			color(midblue*0.6) lcolor(midblue) ///
			title("Period `i'", size(medium)) ///
			xtitle("Emissions / allocation (%)") ///
			plotregion(color(white)) bgcolor(white) graphregion(color(white)) ///
			name("graph_`i'", replace)

		restore

	}
	
	graph combine graph_1 graph_2 graph_3 graph_4 graph_5 graph_6 graph_7 graph_8 graph_9 graph_10, ///
		colfirst cols(2) ysize(10) xsize(6) xcommon ycommon ///
		graphregion(fcolor(white)) plotregion(fcolor(white))
		
	graph export "$TRADING_FIGS/Figure_3.pdf", replace

}

********************************************************************************
*** Emission / Total Holdings
********************************************************************************
if "`figure'" == "4" {
	
	*** Force values of 100 to be just below 100 to highlight non compliers
	gen emission_per_holding_hist = emission_per_holding - 0.00001
	replace emission_per_holding_hist = 0 if emission_per_holding == 0

	forval i=1/10 {
		
		preserve
		
		keep if period_num == `i'
		
		** Drop if emission is negative
		keep if emission_per_holding >= 0
		
		** Truncate at 135%
		keep if emission_per_holding < 135
		
		hist emission_per_holding_hist, ///
			width(10) start(0) xlabel(0(20)140)  ///
			color(midblue*0.6) lcolor(midblue) ///
			title("Period `i'", size(medium)) ///
			xtitle("Emissions / total holding (%)") ///
			plotregion(color(white)) bgcolor(white) graphregion(color(white)) ///
			name("graph_`i'", replace)
			
		restore
		
	}
	
	graph combine graph_1 graph_2 graph_3 graph_4 graph_5 graph_6 graph_7 graph_8 graph_9 graph_10, ///
		colfirst cols(2) ysize(10) xsize(6) xcommon ycommon ///
		graphregion(fcolor(white)) plotregion(fcolor(white))
	
	graph export "$TRADING_FIGS/Figure_4.pdf", replace

}

if "`figure'" == "F3" {
	* Use unadjusted emissions
	
	*** Force values of 100 to be just below 100 to highlight non compliers
	gen emission_per_holding_hist = emission_per_holding - 0.00001
	replace emission_per_holding_hist = 0 if emission_per_holding == 0

	forval i=1/10 {
		
		preserve
		
		keep if period_num == `i'
		
		** Drop if emission is negative
		keep if emission_per_holding >= 0
		
		** Set ratio greater than 200% to 200.1 to display on graph in >200 bar 
		replace emission_per_holding_hist = cond(emission_per_holding_hist > 200 & !missing(emission_per_holding_hist), 200.1, emission_per_holding_hist)
		
		hist emission_per_holding_hist, ///
			width(10) start(0) xlabel(0 "0" 25 "25" 50 "50" 75 "75" 100 "100" 125 "125" 150 "150" 175 "175" 200 ">200") ///
			color(midblue*0.6) lcolor(midblue) ///
			title("Period `i'", size(medium)) ///
			xtitle("Emissions / total holding (%)") ///
			plotregion(color(white)) bgcolor(white) graphregion(color(white)) ///
			name("graph_`i'", replace) width(12.5)
			
		restore
		
	}
	
	graph combine graph_1 graph_2 graph_3 graph_4 graph_5 graph_6 graph_7 graph_8 graph_9 graph_10, ///
		colfirst cols(2) ysize(10) xsize(6) xcommon ycommon ///
		graphregion(fcolor(white)) plotregion(fcolor(white))
	
	graph export "$TRADING_FIGS/Figure_F3.pdf", replace

}
